package com.jinxin.platform.cdzxkacs.rpc;

import com.jinxin.platform.base.common.annotation.RegBusiness;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdzxkacs.bean.CdzxkacsDevice;
import com.jinxin.platform.cdzxkacs.common.Constant;
import com.jinxin.platform.cdzxkacs.mapper.CdzxkacsDeviceMapper;
import com.jinxin.platform.cdzxkacs.mapper.CdzxkacsOpenRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2020-08-24
 * @Author: Lingnan
 **/
@RegBusiness(moduleKey = Constant.MODULE_KEY, className = "OpenDoorRpcService")
@Slf4j
@Service
public class OpenDoorRpcService {
    @Autowired
    private CdzxkacsOpenRecordMapper openRecordMapper;
    @Autowired
    private CdzxkacsDeviceMapper deviceMapper;
    /**
     * 根据用户id 时间等查询开门记录
     * @param param
     * @return
     */
    public JsonResult getOpenRecordCountById(Map param){
        String startDate = param.containsKey("startTime") ? (String)param.get("startTime") : "";
        String endDate = param.containsKey("endTime") ? (String)param.get("endTime") : "";
        String userId = param.containsKey("userId") ? (String) param.get("userId") : "";
        HashMap param2 = new HashMap();
        param2.put("startDate",startDate);
        param2.put("endDate",endDate);
        param2.put("openUserId",userId);
        int count = openRecordMapper.listPageCount(param2);
        return JsonResult.ok(String.valueOf(count));
    }

    /**
     * 查询最新的10条进出记录
     * @param param 参数
     * @return
     */
    public JsonResult getTop10OpenRecords(Map param){
        HashMap param2 = new HashMap();
        param2.put("page", 1);
        param2.put("limit", 10);
        return JsonResult.ok(openRecordMapper.listPage(param2));
    }

    /**
     * 根据设备mac获取设备外网ip账号密码
     * @param param
     * @return
     */
    public JsonResult getDeviceInfoByMac(Map param){
        String mac = param.containsKey("mac") ? (String)param.get("mac") : "";
        if(StringUtils.isEmpty(mac)){
            return JsonResult.error("mac不存在");
        }
        HashMap param2 = new HashMap();
        param2.put("deviceMac", mac);
        return JsonResult.ok(deviceMapper.selectOne(param2));
    }

}
